home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (c) 2009 Bui Viet Thanh (thanhbv@gmail.com).
- *
- * This file is part of clicknlearn.
- *
- * clicknlearn is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * clicknlearn is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with clicknlearn. If not, see <http://www.gnu.org/licenses/>.
- */
-
- let CNLUtils = {
- /**
- * search(vector: Array, value: Object, [insert: Boolean = false]): Integer
- * Do a binary search on an *ordered* array, if it's not ordered, the behaviour is undefined.
- * The function can return the index of the searched object as well the the index where it should be
- * inserted to keep the array ordered.
- * @param o vector
- * array that will be looked up
- * @param v value
- * object that will be searched
- * @param i insert
- * if true, the function will return the index where the value should be inserted
- * to keep the array ordered, otherwise returns the index where the value was found
- * or -1 if it wasn't found
- * Carlos R. L. Rodrigues
- * http://jsfromhell.com/array/search [rev. #2]
- */
- search: function(o, v, i){
- var h = o.length, l = -1, m;
- while(h - l > 1)
- if(o[m = h + l >> 1] < v) l = m;
- else h = m;
- return o[h] != v ? i ? h : -1 : h;
- },
-
- formatWord: function(word){
- word = word.replace(/^ +| +$/g, ""); // Remove empty spaces around words
- word = word.replace(/^'+/g, ""); // Remove ' or � at the start of the word
- word = word.replace(/'+$|�+$/g, ""); // Remove ' or � at the end of the word
- word = word.replace(/\.|,|"|:|;|!|\?|\(|\)|\//g, "");
- return word.toLowerCase();
- },
- /**
- * replace '%%' in str by element in array arr
- */
- replaceString: function(str, arr){
- return str.split(/%%/).reduce(function(previousValue, currentValue, index, array){
- return previousValue + arr[index-1] + currentValue ;
- });
- },
- /**
- * ANT-style variable replacing in strings
- */
- replaceAntStyleString: function(str){
- var re = /\$\{(.+?)\}/mg;
- var reArr;
- var ret = "";
- var prevIndex = 0;
- while ((reArr = re.exec(str)) != null){
- // ex: str = "1${a}2"
- // then property = reArr[1] = "a";
- // & reArr[0] = ${a}
- ret += str.substring(prevIndex, re.lastIndex - reArr[0].length) +
- CNL.stringsBundle.getString(reArr[1]);
- prevIndex = re.lastIndex;
- }
- ret += str.substring(prevIndex);
- return ret;
- },
- getString: function(str, arr){
- let temp = CNLUtils.replaceAntStyleString(str);
- return CNLUtils.replaceString(temp, arr);
- },
-
- VALID_CHARS: /[0-9a-zA-Zа-яА-Яà-žΑ-ῥԱ-ևぁ-ヶ促-杁ㄱ-하ⁿ'’ſÁÉ-]/,
-
- trim: function(str, chars) {
- return this.ltrim(this.rtrim(str, chars), chars);
- },
-
- ltrim: function(str, chars) {
- chars = chars || "\\s";
- return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
- },
-
- rtrim: function(str, chars) {
- chars = chars || "\\s";
- return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
- }
- };
-